#!/usr/bin/env Rscript

library(readr)
library(dplyr)

# TODO rename functions
# This one generates a list for a tree or dendrogram
rsplit <- function(x) {
  if(ncol(x) > 1) {
      listSplit <- split(x[-c(1)], x[1], drop=F)
      lapply(names(listSplit), function(y) {list(name = y,
                                                 children = rsplit(listSplit[[y]]))})
  } else {
      lapply(seq(nrow(x[1])), function(y) {list(name = as.character(x[y,1]))})
  }
}

# This one generates a list for a treemap
makeList <- function(x) {
  if(ncol(x) > 2) {
      listSplit <- split(x[-c(1)], x[1], drop=F)
      lapply(names(listSplit), function(y) {list(name = y,
                                                 value = as.integer(listSplit[[y]][1,1]),
                                                 children = makeList(listSplit[[y]][-1]))})
  } else {
      lapply(seq(nrow(x[1])), function(y) {list(name = as.character(x[y,1]),
                                                value = as.integer(x[y,2]))})
  }
}

make_bardata <- function(x) {
      listSplit <- split(x[-1], x[1], drop=F)
      lapply(names(listSplit), function(y) {
          list(name = y,
               value = as.vector(filter(x, x[1] == y)$n))
      })
}

make_bardata2 <- function(x) {
      apply(unique(x[c(1, 2)]), 1, function(y) {
          list(name = c(as.character(y[1]), as.character(y[2])),
               value = as.vector(filter(x, x[1] == y[1], x[2] == y[2])$n))
      })
}

row_to_list  <- function(d) {
    apply(d, 1, function(x) {
        setNames(as.list(x), c("axis", "value"))
    })
}

make_radardata <- function(x) {
    listSplit <- split(x[-c(1)], x[1], drop=F)
    lapply(names(listSplit), function(y) {
        list(name = y,
             value = row_to_list(filter(x, x[1] == y)[-1]))
    })
}

make_radardata2 <- function(x) {
    apply(unique(x[c(1, 2)]), 1, function(y) {
        list(name = c(as.character(y[1]), as.character(y[2])),
             value = row_to_list(filter(x, x[1] == y[1], x[2] == y[2])[-c(1,2)]))
    })
}

make_hm_list <- function(x) {
    apply(x, 1, function(y) {
        list(uf = as.character(y[1]),
             month = as.integer(y[2]),
             day = as.integer(y[3]),
             value = as.integer(y[4]))
    })

}


print("loading data...")
brasil <- read_csv2("../app/data/brasil.csv")
acidentes <- read_csv2("../app/data/acidentes.csv")

#barchart data
pais_bar <- read_csv2("../app/data/barchart/ac_pais_2012.csv")
regioes_bar <- read_csv2("../app/data/barchart/ac_regiao_2012.csv")
estados_bar <- read_csv2("../app/data/barchart/ac_uf_2012.csv")
meso_bar <- read_csv2("../app/data/barchart/ac_meso_2012.csv")
micro_bar <- read_csv2("../app/data/barchart/ac_micro_2012.csv")
municipios_bar <- read_csv2("../app/data/barchart/ac_mun_2012.csv")

#radarchart data
pais_radar <- read_csv2("../app/data/radarchart/pais_by_year.csv")
regioes_radar <- read_csv2("../app/data/radarchart/regiao_by_year.csv")
estados_radar <- read_csv2("../app/data/radarchart/uf_by_year.csv")
meso_radar <- read_csv2("../app/data/radarchart/meso_by_year.csv")
micro_radar <- read_csv2("../app/data/radarchart/micro_by_year.csv")
municipios_radar <- read_csv2("../app/data/radarchart/mun_by_year.csv")

#heatmap_table data
heatmap_data_csv_2012<- read_csv2(file = "../app/data/heatmap/estados_mes_dia_2012.csv", na = "NA")
heatmap_data_csv_2013<- read_csv2(file = "../app/data/heatmap/estados_mes_dia_2013.csv", na = "NA")
heatmap_data_csv_2014<- read_csv2(file = "../app/data/heatmap/estados_mes_dia_2014.csv", na = "NA")
heatmap_data_csv_2015<- read_csv2(file = "../app/data/heatmap/estados_mes_dia_2015.csv", na = "NA")
heatmap_data_csv_2016<- read_csv2(file = "../app/data/heatmap/estados_mes_dia_2016.csv", na = "NA")
heatmap_data_csv_2017<- read_csv2(file = "../app/data/heatmap/estados_mes_dia_2017.csv", na = "NA")

#print("Computing scatterplot data...")
#sct_data <- as.matrix(data[ , c("hora_acidente", "idade_cat", "ds_agente_causador")])

print("Computing choropleth map data...")
choropleth_data <- read_csv2("../app/data/puf.csv")

print("Computing parallel coordinates data...")
parallelcoordinates_data <- read_csv2("../app/data/acidentes-sumarizado.csv")

print("Computing barchart data...")
pais_bar  <- make_bardata(pais_bar)
regioes_bar  <- make_bardata2(regioes_bar)
estados_bar  <- make_bardata2(estados_bar)
meso_bar  <- make_bardata2(meso_bar)
micro_bar  <- make_bardata2(micro_bar)
municipios_bar  <- make_bardata2(municipios_bar)

bar_data  <- c(pais_bar, regioes_bar, estados_bar,
               meso_bar, micro_bar, municipios_bar)

print("Computing radarchart data...")
pais_radar  <- make_radardata(pais_radar)
regioes_radar  <- make_radardata2(regioes_radar)
estados_radar  <- make_radardata2(estados_radar)
meso_radar  <- make_radardata2(meso_radar)
micro_radar  <- make_radardata2(micro_radar)
municipios_radar  <- make_radardata2(municipios_radar)

radar_data  <- c(pais_radar, regioes_radar, estados_radar,
                 meso_radar, micro_radar, municipios_radar)

print("Computing treemap data...")
treemap_data <- makeList(unique(acidentes))

print("Computing dendrogram data...")
tree_data <- rsplit(unique(brasil))[[1]]

print("Reading Choropleth mesorregion csv data...")
percent_est12  <- read_csv2("../app/data/percentage_meso2012.csv")
percent_est13  <- read_csv2("../app/data/percentage_meso2013.csv")
percent_est14  <- read_csv2("../app/data/percentage_meso2014.csv")
percent_est15  <- read_csv2("../app/data/percentage_meso2015.csv")
percent_est16  <- read_csv2("../app/data/percentage_meso2016.csv")
percent_est17  <- read_csv2("../app/data/percentage_meso2017.csv")

print("Computing heatmap_table data...")
heatmap_2012_data <- make_hm_list(heatmap_data_csv_2012)
heatmap_2013_data <- make_hm_list(heatmap_data_csv_2013)
heatmap_2014_data <- make_hm_list(heatmap_data_csv_2014)
heatmap_2015_data <- make_hm_list(heatmap_data_csv_2015)
heatmap_2016_data <- make_hm_list(heatmap_data_csv_2016)
heatmap_2017_data <- make_hm_list(heatmap_data_csv_2017)

print("Writing out generated R objects...")
#saveRDS(sct_data, "../app/data/rds/sct_data.rds")
saveRDS(choropleth_data, "../app/data/rds/choropleth_data.rds")
saveRDS(parallelcoordinates_data, "../app/data/rds/parallelcoordinates_data.rds")
saveRDS(bar_data, "../app/data/rds/bar_data.rds")
saveRDS(radar_data, "../app/data/rds/radar_data.rds")
saveRDS(treemap_data, "../app/data/rds/treemap_data.rds")
saveRDS(tree_data, "../app/data/rds/tree_data.rds")

saveRDS(heatmap_2012_data, "../app/data/rds/heatmap_2012_data.rds")
saveRDS(heatmap_2013_data, "../app/data/rds/heatmap_2013_data.rds")
saveRDS(heatmap_2014_data, "../app/data/rds/heatmap_2014_data.rds")
saveRDS(heatmap_2015_data, "../app/data/rds/heatmap_2015_data.rds")
saveRDS(heatmap_2016_data, "../app/data/rds/heatmap_2016_data.rds")
saveRDS(heatmap_2017_data, "../app/data/rds/heatmap_2017_data.rds")

saveRDS(percent_est12, "../app/data/rds/percentage_meso2012.rds")
saveRDS(percent_est13, "../app/data/rds/percentage_meso2013.rds")
saveRDS(percent_est14, "../app/data/rds/percentage_meso2014.rds")
saveRDS(percent_est15, "../app/data/rds/percentage_meso2015.rds")
saveRDS(percent_est16, "../app/data/rds/percentage_meso2016.rds")
saveRDS(percent_est17, "../app/data/rds/percentage_meso2017.rds")

print("Done!")
